package com.secret;



import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



// http://www.cnblogs.com/freeliver54/archive/2011/10/08/2202136.html
// AES是对称加密
public class aes {

	/**
	* 加密
	*/
	public static byte[] encrypt(String content, String password) {
	   try {
	    KeyGenerator kgen = KeyGenerator.getInstance("AES");
	    kgen.init(128, new SecureRandom(password.getBytes()));
	    SecretKey secretKey = kgen.generateKey();
	    byte[] enCodeFormat = secretKey.getEncoded();
	    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
	    Cipher cipher = Cipher.getInstance("AES");// 创建密码器
	    byte[] byteContent = content.getBytes("utf-8");
	    cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 加密模式
	    byte[] result = cipher.doFinal(byteContent);
	    return result; // 加密
	   } catch (NoSuchAlgorithmException e) {
	    e.printStackTrace();
	   } catch (NoSuchPaddingException e) {
	    e.printStackTrace();
	   } catch (InvalidKeyException e) {
	    e.printStackTrace();
	   } catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	   } catch (IllegalBlockSizeException e) {
	    e.printStackTrace();
	   } catch (BadPaddingException e) {
	    e.printStackTrace();
	   }
	   return null;
	}
	/**
	* 解密
	* 
	* @param content
	* @param password
	* @return
	*/
	public static byte[] decrypt(byte[] content, String password) {
	   try {
	    KeyGenerator kgen = KeyGenerator.getInstance("AES");
	    kgen.init(128, new SecureRandom(password.getBytes()));
	    SecretKey secretKey = kgen.generateKey();
	    byte[] enCodeFormat = secretKey.getEncoded();
	    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
	    Cipher cipher = Cipher.getInstance("AES");// 创建密码器
	    cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 解密模式 !!!
	    byte[] result = cipher.doFinal(content);
	    return result; // 解密
	   } catch (NoSuchAlgorithmException e) {
	    e.printStackTrace();
	   } catch (NoSuchPaddingException e) {
	    e.printStackTrace();
	   } catch (InvalidKeyException e) {
	    e.printStackTrace();
	   } catch (IllegalBlockSizeException e) {
	    e.printStackTrace();
	   } catch (BadPaddingException e) {
	    e.printStackTrace();
	   }
	   return null;
	}
	/**
	* @param args
	 * @throws UnsupportedEncodingException 
	*/
	public static void main(String[] args) throws UnsupportedEncodingException {
	   //需要加密的内容
	   //String content = "加密的内容" ;
		String content = "" ;
		for(int i =0; i<16; i++){
			content +=""+i;
		}
	   //加密密码
	   String password = "123456" ;
	   byte[] encryptContent = encrypt(content, password);
	   byte[] decryptContent = decrypt( encryptContent, password); //decrypt(encryptContent , password);
	   try {
		  if(decryptContent != null)
			  System.out.println(new String(decryptContent , "utf-8"));
	   } catch (UnsupportedEncodingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	   }
	}
	
	
	
}
